<template>
  <div>
    <!-- from表单 -->
    <el-dialog
      title="修改成绩"
      :visible.sync="show"
      width="50%"
      :before-close="closeDialog"
    >
      <div class="from-infomations">
        <el-form ref="form" label-position="left" :model="userInfo" label-width="10rem">
          <el-form-item label="工号">
            {{ userInfo.userId }}
          </el-form-item>
          <el-form-item label="姓名">
            {{ userInfo.userinfo.userName }}
          </el-form-item>
          <el-form-item label="所属党小组">
            {{ userInfo.userinfo.userBranch }}
          </el-form-item>

        </el-form>
      </div>
      <!-- 还原信息(待添加) -->

      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取 消</el-button>
        <el-button type="primary" @click="sendDialog()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { updateUserInfo, updateUser } from '../../api/user.js'
export default {
  props: {
  },
  data() {
    return {
      // 获取的动态数据
      userInfo: {
        userId: '',
        userPwd: '',
        userType: '',
        userTypeName: '',
        userinfo: {
          userName: '',
          userBranch: '',
          userReconsider: '',
          userScore: ''
        }
      },

      // 页面存储数据
      show: false

    }
  },
  created() {
    // dialog信息初始化
    this.createDialogStatus()
  },
  methods: {
    // dialog信息初始化
    createDialogStatus() {
      this.show = false
    },
    // 展示dialog
    showDialog(data) {
      this.show = true
      var that = this
      //  获取用户信息
      that.userInfo = data
    },
    // 关闭dialog
    closeDialog() {
      this.$confirm('是否确定取消', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.show = false
        this.$message({
          type: 'success',
          message: '取消修改成功'
        })
      }).catch(() => {})
    },
    // 提交dialog
    sendDialog() {
      var that = this
      // 做拦截
      if (that.userInfo.userinfo.userName === '') {
        that.$message({
          type: 'warning',
          message: '请输入姓名后提交'
        })
      } else {
        new Promise(function(resolve, reject) {
          that.$confirm('是否确定提交', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            that.$message('正在提交中')
            that.updateUserInfo()
            resolve()
          }).catch(() => {
            resolve()
          })
        }).then(() => {

        })
      }
    },
    // 更新userInfo信息
    updateUserInfo() {
      // 获取全部数据
      var that = this
      var userInfo = that.userInfo
      var userUpdateInfo = {
        userId: '',
        userName: '',
        userReconsider: '',
        userScore: '',
        userBranch: '',
        user: {
          userId: '',
          userPwd: '',
          userType: ''
        }
      }
      // 更新信息
      userUpdateInfo.userId = userInfo.userId
      userUpdateInfo.userName = userInfo.userinfo.userName
      userUpdateInfo.userReconsider = userInfo.userinfo.userReconsider
      userUpdateInfo.userScore = userInfo.userinfo.userScore
      userUpdateInfo.userBranch = userInfo.userinfo.userBranch
      userUpdateInfo.userPwd = userInfo.userPwd
      userUpdateInfo.userType = userInfo.userType
      new Promise(function(resolve, reject) {
        updateUserInfo(userUpdateInfo).then(res => {
          resolve(res.msg)
        })
      }).then((data) => {
        if (data === '操作成功') {
          updateUser(userUpdateInfo).then(res => {
            if (res.obj === null) {
              that.show = false
              that.$message({
                message: '修改成功',
                type: 'success'
              })
              top.location.reload()
            } else {
              that.$message({
                message: '服务器异常，请稍后重试',
                type: 'success'
              })
            }
          })
        } else {
          that.$message({
            message: '服务器异常，请稍后重试',
            type: 'success'
          })
        }
      })
    }

  }
}
</script>
<style lang="scss">

</style>
